package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：
 *
 * @package: cn.wolfcode.trip.api.controller
 * @author: Patch
 * @date: 2019/5/19 11:34
 * 总结:
 */
@RestController
@RequestMapping("images")
public class ImageController {
    /**
     * 上传图片解接口
     * 1.资源 /images
     * 2.请求方式 post
     * 3.请求参数 file
     * 4.返回结果 图片的地址
     */
    @PostMapping
    public Map upload(MultipartFile upload) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            String imgUrl = UploadUtil.uploadQiniu(upload);
            map.put("uploaded", 1);
            map.put("url", imgUrl);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("uploaded", 0);
            Map<String, Object> temp = new HashMap<>();
            temp.put("message", e.getMessage());
            map.put("error", temp);
        }
        return map;
    }
}
